//
//  SINATecScoreQueryDetailListViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/8.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecScoreQueryDetailListViewController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINATecScoreQueryDetailCourseViewController.h"
#import "SINATecScoreQueryDetailUserNameViewController.h"

@interface SINATecScoreQueryDetailListViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

@property(strong,nonatomic) NSArray *topDataList;

@property(strong,nonatomic)  UISegmentedControl *segMente;
@end

@implementation SINATecScoreQueryDetailListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:0 target:self action:@selector(dismiss)];
    //backItem.title = @"返回";
    backItem.image = [UIImage imageNamed:@"back1"];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
    //先生成存放标题的数据
    NSArray *array = [NSArray arrayWithObjects:@"课程",@"姓名", nil];
    //初始化UISegmentedControl
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
    //设置frame
    segment.frame = CGRectMake(mainW/2-60, 100, 120, 30);
    //开始时默认选中下标(第一个下标默认是0)
    segment.selectedSegmentIndex = 0;
    //添加事件
    [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    self .segMente = segment;
    //添加到视图
    self.navigationItem.titleView = segment;
    UIFont *font = [UIFont boldSystemFontOfSize:16.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                           forKey:NSFontAttributeName];
    [segment setTitleTextAttributes:attributes
                               forState:UIControlStateNormal];
    
    [self initOtherController];
    
}


//点击不同分段就会有不同的事件进行相应
-(void)change:(UISegmentedControl *)sender{
   
    if (sender.selectedSegmentIndex == 0) {
        DLog(@"课程");
    }else if (sender.selectedSegmentIndex == 1){
        DLog(@"姓名");
    }
    
    CGPoint point = CGPointMake(sender.selectedSegmentIndex*mainW, self.contentScrollView.contentOffset.y);
    [self.contentScrollView setContentOffset:point animated:YES];
}
-(void)dismiss{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSArray *)topDataList{
    if (!_topDataList) {
        _topDataList= @[@"课程",@"姓名"];
    }
    return _topDataList;
}
-(void)initOtherController{
    NSArray *array = @[@"SINATecScoreQueryDetailCourseViewController",@"SINATecScoreQueryDetailUserNameViewController"];
    for (NSInteger i=0; i<array.count; i++) {
        //取出数据
        NSString *item = array[i];
        //创建
        UIViewController *controller = [[NSClassFromString(item) alloc] init];
        //设置标题
        
        if ([controller isKindOfClass:[SINATecScoreQueryDetailCourseViewController class]]) {
            ((SINATecScoreQueryDetailCourseViewController*)controller).pageType=self.pageType;
            ((SINATecScoreQueryDetailCourseViewController*)controller).model=self.model;
        }
        
        if ([controller isKindOfClass:[SINATecScoreQueryDetailUserNameViewController class]]) {
            ((SINATecScoreQueryDetailUserNameViewController*)controller).pageType=self.pageType;
            ((SINATecScoreQueryDetailUserNameViewController*)controller).model=self.model;
        }
        //controller.title = self.topDataList[i];
    
        [self addChildViewController:controller];
    }
    //将子控制器的View添加到MainView的scrollView中
    //4 设置scrollview的contentsize
    self.contentScrollView.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width*self.topDataList.count, 0);
    //5 设置uiscrollView的代理
    self.contentScrollView.delegate=self;
    self.contentScrollView.pagingEnabled=YES;
    //进入主控制器的第一个页面
    [self scrollViewDidEndDecelerating:self.contentScrollView];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    //每一个子控制器的宽度
    CGFloat width =scrollView.frame.size.width;
    //每一个子控制器的高度
    CGFloat height =CGRectGetHeight(self.contentScrollView.frame);
    //获取当前索引值
    NSInteger idx = scrollView.contentOffset.x/width;
    
    //设置联动
    self.segMente.selectedSegmentIndex =idx;
    //[self.topBarView scrollChangTopBar:idx];
    //根据索引值返回引用
    UIViewController *vc=self.childViewControllers[idx];
    //判断当前的vc是否执行过viewLoaded
    if([vc isViewLoaded]){
        return;
    }else{
        //设置子控制器的视图大小
        vc.view.frame=CGRectMake(scrollView.contentOffset.x, 0, scrollView.frame.size.width, height);
        //将子控制器的view加入到scrollview中
        [scrollView addSubview:vc.view];
    }
}
//滑动减速结束
//调用加载子控制器的view的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //DLog(@"滑动结束 ...");
    [self scrollViewDidEndScrollingAnimation:scrollView];
}
//scrollView滚动时，就调用该方法。任何offset值改变都调用该方法。即滚动过程中，调用多次

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //DLog(@"scrollViewDidScroll");
    
//    CGPoint point=scrollView.contentOffset;
    
    //DLog(@"%f,%f",point.x,point.y);
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
